import Foundation
import UIKit
 

class ViewController: UIViewController,UICollectionViewDelegate,
                      UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DefaultCell.MyCellButtonDelegate{

    private var collectionView:UICollectionView!
    private var items:[(btnTitle:String,btnColor:UIColor)]=[]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //导航栏
//        self.navigationController?.navigationBar.backgroundColor = .white;
//        self.navigationItem.title = "隐私管理";
//        let backButton = UIBarButtonItem(image: UIImage(systemName: "chevron.left")?.withTintColor(.gray,renderingMode: .alwaysOriginal),
//                                            style: .plain,
//                                            target: self,
//                                            action: #selector(backAction))
//        navigationItem.leftBarButtonItem = backButton;
        
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .vertical;
        collectionView=UICollectionView(frame: view.bounds,collectionViewLayout: layout);
        collectionView?.backgroundColor = .white;
        collectionView?.dataSource = self;
        collectionView?.delegate=self;
        collectionView?.register(DefaultCell.self, forCellWithReuseIdentifier: "DefaultCell");
        collectionView?.register(MyHeaderView.self,
                                 forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                                 withReuseIdentifier: "MyHeader")
//        items.append(("静态页面",UIColor.blue));
//        items.append(("测试页面",UIColor.green));
//        items.append(("输入文本",UIColor.yellow));
//        items.append(("颜色块",UIColor.gray));
//        items.append(("嵌套Collection",UIColor.orange));
        items.append(("蓝湖页面",UIColor.systemPink));
//        items.append(("测试页面6",UIColor.cyan));
        items.append(("成长值页",UIColor.brown));
        view.addSubview(collectionView);
    }
    
    
    @objc private func backAction() {
        navigationController?.popViewController(animated: true)
    }
    // MARK: - DataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DefaultCell", for: indexPath) as! DefaultCell;
        let item = items[indexPath.row]
        cell.configure(btnTitle:item.btnTitle,btnColor:item.btnColor )
        cell.backgroundColor = item.btnColor
        return cell;
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let item = items[indexPath.row]
        if item.btnTitle == "静态页面" {
            goToStaticView()
        }else if item.btnTitle == "测试页面"{
            goToTestViewController()
        }else if item.btnTitle == "输入文本" {
            goToTestViewController2()
        }else if item.btnTitle == "颜色块" {
            goToTestViewController3()
        }else if item.btnTitle == "嵌套Collection" {
            goToTestViewController4()
        }else if item.btnTitle == "蓝湖页面" {
            goToTestViewController5()
        }else if item.btnTitle == "测试页面6" {
            goToTestViewController6()
        }else if item.btnTitle == "成长值页" {
            goToTestViewController7()
        }
    }
    
    // MARK: - Header 配置
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: "MyHeader",
                for: indexPath
            ) as! MyHeaderView
//            header.title = "区头：Section \(indexPath.section)"
            return header
        }
        return UICollectionReusableView()
    }
    
    // MARK: - DelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.bounds.width)
        let imageHeight = 16.0;
        let textHeight:CGFloat = 40;
        //高度为宽度的 1.5 倍
        return CGSize(width: width, height: imageHeight + textHeight + 24);
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        //设置 Header 的固定高度（50pt）和宽度（填充整个 collectionView）
        return CGSize(width: collectionView.bounds.width, height: 0)
    }
    func didTapButton(in cell: DefaultCell) {
        let item = cell.cellButton
        let btntitle = item.currentTitle
        print("按钮跳转")
        if btntitle == "静态页面" {
            goToStaticView()
        }else if btntitle == "测试页面"{
            goToTestViewController()
        }
    }
    @objc func goToStaticView() {
        let vc = StaticView()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController(){
        let vc = TestViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController2(){
        let vc = TextTest()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController3(){
        let vc = ColorBlockTest()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController4(){
        let vc = NestedCollection()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController5(){
        let vc = LHPage()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController6(){
        let vc = TestViewController6()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc func goToTestViewController7(){
        let vc = GrowthValueController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
